/**
 * 
 */
package cn.ac.istic.lkt.atv;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
 * @Author shicd
 * @Date 2009-7-19
 * 
 */
public class TreePanelSingle extends TreePanelAbstract {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3608874166315266650L;
	private ParseTree pt;
//	private float des2node; // 字符高度＋连线垂直高度

	public TreePanelSingle() {
		super();
		
	}

	public void init(ParseTree pt){
		this.pt = pt;
	}
	public void myPaint(Graphics2D g2){
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setFont(TFONT);
		FontRenderContext frc = g2.getFontRenderContext();
		charHeight = (float) TFONT.getMaxCharBounds(frc).getHeight();
		charWidth =((float) TFONT.getMaxCharBounds(frc).getWidth());

		if (this.pt == null) {
			g2.drawString("trees", 10, 20);
			return;
		}
		calcPos(frc);
		List<PTNode> nodelist = pt.getNodeList();
		for (int i = 0; i < nodelist.size(); i++) {
			PTNode nd = nodelist.get(i);
			if (nd.isWord()){
				g2.setFont(TFONT);
			}
			else g2.setFont(TFONT);
			//System.out.println(nodeString[i]+" "+nodeX[i]+" "+nodeY[i]);
			g2.drawString(nd.getLabel(), nd.getX(), nd.getY());
		}
		drawLines(g2,pt,nodelist.get(0));		
	}
	
	
	public void calcPos(FontRenderContext frc ){
		pt.setX(frc);// 设置每一个树节点的X坐标
		pt.setY(frc); // 设置每一个树节点的Y坐标
		imgWidth = (int)(pt.getMaxX()+ STARTX);
		imgHeight = (int )(pt.getMaxY() + STARTX);
	}
	
	public Dimension getPreferredSize() {
        return new Dimension(imgWidth,imgHeight);
    }

	public void saveImage() {

		BufferedImage bi = (BufferedImage) this.createImage(this
				.getPreferredSize().width, this.getPreferredSize().height);
		paint(bi.getGraphics());
		try {
			File directory = new File(".");
			javax.imageio.ImageIO.write(bi, "bmp", new File(directory
					.getCanonicalPath()
					+ "/gant.bmp"));
		} catch (IOException e1) {
			// TODO 自动生成 catch 块
			e1.printStackTrace();
		}
	}


	public float getCharWidth() {
		return charWidth;
	}

	public ParseTree getPt() {
		return pt;
	}

	public void setPt(ParseTree pt) {
		this.pt = pt;
	}



	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ParseTree pt = new ParseTree(
				"( (IP-HLN (NP-SBJ (NP-PN (NR 上海) (NR 浦东))  (NP (NN 开发) (CC 与) (NN 法制) (NN 建设))) (VP (VV 同步))) ) ");
		// pt.Parse();

		TreePanelSingle csp = new TreePanelSingle();	
		csp.init(pt);
		JFrame jf = new JFrame();
		JScrollPane scrollpane = new JScrollPane(csp);
		// scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		// scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		jf.getContentPane().add(scrollpane);
		jf.pack();
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jf.setVisible(true);
		// csp.saveImage();
	}
}
